package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockBillMapper;
import cc.mcggdd.leah.boot.stock.service.StockBillService;
import cc.mcggdd.leah.boot.stock.model.entity.StockBill;
import cc.mcggdd.leah.boot.stock.model.form.StockBillForm;
import cc.mcggdd.leah.boot.stock.model.query.StockBillQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockBillVO;
import cc.mcggdd.leah.boot.stock.converter.StockBillConverter;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 账单服务实现类
 *
 * @author 
 * @since 2025-02-10 14:57
 */
@Service
@RequiredArgsConstructor
public class StockBillServiceImpl extends ServiceImpl<StockBillMapper, StockBill> implements StockBillService {

    private final StockBillConverter stockBillConverter;

    /**
    * 获取账单分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockBillVO>} 账单分页列表
    */
    @Override
    public IPage<StockBillVO> getStockBillPage(StockBillQuery queryParams) {
        Page<StockBillVO> pageVO = this.baseMapper.getStockBillPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取账单表单数据
     *
     * @param id 账单ID
     * @return
     */
    @Override
    public StockBillForm getStockBillFormData(Long id) {
        StockBill entity = this.getById(id);
        return stockBillConverter.toForm(entity);
    }
    
    /**
     * 新增账单
     *
     * @param formData 账单表单对象
     * @return
     */
    @Override
    public boolean saveStockBill(StockBillForm formData) {
        StockBill entity = stockBillConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新账单
     *
     * @param id   账单ID
     * @param formData 账单表单对象
     * @return
     */
    @Override
    public boolean updateStockBill(Long id,StockBillForm formData) {
        StockBill entity = stockBillConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除账单
     *
     * @param ids 账单ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockBills(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的账单数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public BigDecimal getStockTotalAmount(Long debtId, String status) {
        return this.baseMapper.getStockTotalAmount(debtId, status);
    }

    @Override
    public LocalDate getRepayMaxDate() {
        return this.baseMapper.getRepayMaxDate("N");
    }

    @Override
    public List<StockBill> getAllRecord(String status) {
        QueryWrapper<StockBill> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(status)) {
            queryWrapper.eq("status", status);
        }
        return this.baseMapper.selectList(queryWrapper);
    }

}
